package com.tungkong.usbcamera.view;

import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tungkong.usbcamera.R;
import com.tungkong.usbcamera.application.ClientApplication;
import com.tungkong.usbcamera.UVCCameraPreview;

import java.io.File;

/**
 * 创建日期：2021-07-16 15:45
 * 创建者：吴开杰
 * 修改日期：2021-07-16 15:45
 * 修改者：吴开杰
 * 版本：v.1.0
 * 描述：
 * 1.
 * 2.
 * 3.
 */
public class USBMultipleRecordVideoActivity extends AppCompatActivity{

    private String TAG = "MultiUSBRecordVideoActivity";

    private TextView mMsgTV;

    private UVCCameraPreview mCameraHelper;
    private UsbDevice usbDevice1;
    private UsbDevice usbDevice2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_video_multiusb);

        mMsgTV = this.findViewById(R.id.msg);

        mCameraHelper = UVCCameraPreview.getInstance();
        mCameraHelper.setDefaultFrameFormat(UVCCameraPreview.FRAME_FORMAT_MJPEG);
        mCameraHelper.initUSBMonitor(this);

    }

    public void onInitUSB(View view){

        Log.i(TAG, "打开USB摄像头");

        if (mCameraHelper == null) {
            Log.i(TAG, "请初始化USB摄像头");
            return;
        }

//        // 票仓摄像头
//        insideUSB= uvcCameraRecord.getSpecificUSBDevice(1143,34864);
//        // 刷脸摄像头
//        outsideUSB= uvcCameraRecord.getSpecificUSBDevice(11707,518);

        // 前置
        usbDevice1 = mCameraHelper.getUsbDevice(5546,5461);
        // 票仓
        usbDevice2 = mCameraHelper.getUsbDevice(21862,4660);

        if(usbDevice1 != null){
            mCameraHelper.openCamera(usbDevice1);
        } else {
            Log.i(TAG, "该USB摄像头未连接");
        }
        if(usbDevice2 != null){
            mCameraHelper.openCamera(usbDevice2);
        } else {
            Log.i(TAG, "该USB摄像头未连接");
        }

        mMsgTV.setText("摄像头已经打开");

    }

    public void onStartRecord(View view){
        if (usbDevice1 != null) {
            Log.i(TAG, "开始票仓录制");

            // 文件路径
            String videoPath = getFileName(usbDevice1);
            // 开始录制
            mCameraHelper.startPusher(usbDevice1);

            mMsgTV.setText("开始票仓录制");
        }
    }

    public void onStopRecord(View view){
        if (usbDevice1 != null) {
            Log.i(TAG, "结束票仓录制");

            mCameraHelper.stopPusher(usbDevice1);

            mMsgTV.setText("录制状态：停止");

        }
    }

    public void onStartFaceRecord(View view){
        if (usbDevice2 != null) {
            Log.i(TAG, "开始人脸录制");

            // 文件路径
            String videoPath = getFileName(usbDevice2);
            // 开始录制
            mCameraHelper.startPusher(usbDevice2);

            mMsgTV.setText("开始人脸录制");
        }
    }

    public void onStopFaceRecord(View view){
        if (usbDevice2 != null) {

            Log.i(TAG, "结束人脸录制");

            mCameraHelper.stopPusher(usbDevice2);

            mMsgTV.setText("录制状态：停止");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCameraHelper != null){
            mCameraHelper.release(usbDevice1);
            mCameraHelper.release(usbDevice2);
        }
    }

    private String getFileName(UsbDevice usbDevice){
        // 文件名称，不需要添加后缀
        String fileName = "vid"+usbDevice.getVendorId() + "-pid" + usbDevice.getProductId() +"-" + System.currentTimeMillis();
        // 文件路径
        String videoPath = UVCCameraPreview.ROOT_PATH + ClientApplication.DIRECTORY_NAME + File.separator + "USBCamera/videos/" + fileName;
        return videoPath;
    }

}
